package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.adapter2.Zhuliadapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.Helplist;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.view.Wuview.RecyclerViewDivider;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

public class ZhuliActivity extends AppCompatActivity {
    private XRecyclerView xRecyclerView;
    private TextView tvnum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuli);
            initview();
            getHelplist();

    }

    private void getHelplist() {
            String url = ApiManager.HELPLIST + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this);
            Log.e("shaishouyi", "主题url====" + url);
            HttpUtils http = new HttpUtils();
            http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Helplist helplist = new Gson().fromJson(responseInfo.result, Helplist.class);
                if (helplist.getCode().equals("10000")){
                    Helplist.DataBean data = helplist.getData();
                    int total = data.getTotal();
                    if (total<=0){
                        Toast.makeText(ZhuliActivity.this,"还没有人为你助力",Toast.LENGTH_SHORT).show();
                        return;
                    }else {
                        List<Helplist.DataBean.ListBean> list = data.getList();
                        tvnum.setText(data.getTotal());
                        xRecyclerView.setAdapter(new Zhuliadapter(ZhuliActivity.this,list));
                    }

                }else {
                    Toast.makeText(ZhuliActivity.this,helplist.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(ZhuliActivity.this,s,Toast.LENGTH_SHORT).show();

            }
        });

    }

    private void initview() {
        xRecyclerView = (XRecyclerView) findViewById(R.id.xrecyckle);
        tvnum = (TextView) findViewById(R.id.tvnumren);
        xRecyclerView.setPullRefreshEnabled(true);
        xRecyclerView.setLoadingMoreEnabled(true);
        xRecyclerView.addItemDecoration(new RecyclerViewDivider(ZhuliActivity.this, LinearLayoutManager.HORIZONTAL));
        LinearLayoutManager manager = new LinearLayoutManager(ZhuliActivity.this,LinearLayoutManager.VERTICAL,false);
        xRecyclerView.setLayoutManager(manager);

    }
}
